const fileService = require('../service/file.service')
const momentService = require('../service/moment.service')

const { PICTURE_PATH } = require('../constants/file-path')
const fs = require('fs')
class MomentController {
    async create(ctx, next) {
        console.log(ctx.request.body)
        const userId = ctx.user.id
        let { content, images, labels, plateId } = ctx.request.body
        // console.log(labels)
        // console.log(images)

        const result = await momentService.create(userId, content, plateId)

        console.log("创建帖子成功")
        try {
            const momentId = result.insertId
            ctx.params.momentId = momentId
            ctx.labels = labels
            ctx.images = images
            await next()
        } catch (err) {
            console.log(err)
        }

    }

    async detail(ctx, next) {
        const momentId = ctx.params.momentId;

        const result = await momentService.getMomentById(momentId)

        ctx.body = result

    }
    async list(ctx, next) {
        const { offset, size } = ctx.query
        const result = await momentService.getMomentList(offset, size)
        ctx.body = result
    }
    //获取相应板块的动态
    async plateList(ctx, next) {
        const { plateId } = ctx.params
        console.log(plateId)
        const { offset, size } = ctx.query
        const result = await momentService.getPlateMomentList(offset, size, plateId)
        ctx.body = result
    }
    async update(ctx, next) {
        // console.log("moment updata controller")
        const momentId = ctx.params.momentId;
        const content = ctx.request.body.content
        const result = await momentService.updateMomentById(momentId, content)

        ctx.body = result
    }
    async remove(ctx, next) {
        // console.log("moment remove controller")
        const momentId = ctx.params.momentId;
        const result = await momentService.removeMomentById(momentId)

        ctx.body = result
    }
    async addLabels(ctx, next) {
        const { labels } = ctx
        const { momentId } = ctx.params
        for (let label of labels) {
            const isExist = await momentService.hasLabel(momentId, label.id)
            if (!isExist) {
                await momentService.addLabel(momentId, label.id)
            }
        }

        await next()
    }
    async fileInfo(ctx, next) {
        let { filename } = ctx.params
        const fullfilename = `${APP_HOST}:${APP_PORT}/moment/image/${filename}`
        const fileInfo = await fileService.getFileByFilename(fullfilename)
        ctx.response.set('content-type', fileInfo.mimetype)
        ctx.body = fs.createReadStream(`${PICTURE_PATH}/${filename}`)
    }
    async addImages(ctx, next) {
        console.log(ctx.images)
        const { images } = ctx
        const { momentId } = ctx.params
        for (let image of images) {
            await momentService.pushImagesIntoMoment(momentId, image)
        }
        ctx.body = "创建贴子成功~"
    }

}
module.exports = new MomentController()